package eSchool.GUI.Controls;

import eSchool.BL.CriteriosPesquisa;
import eSchool.GUI.Components.IUIConector;
import eSchool.GUI.Components.UIConectorAluno;
import eSchool.GUI.Components.UIConectorDocente;
import eSchool.GUI.Components.UIConectorFuncionario;
import eSchool.Utils.TextHelper;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;

public class panTabFicha extends JPanelExtended {

    private IUIConector uiConector;
    private Boolean idObrigatorio = false;
    private Boolean nomeObrigatorio = false;
    
    public panTabFicha() {
        initComponents();

        panDataGrid1.setEventListner(this);
 }

        
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabConsulta = new javax.swing.JTabbedPane();
        pnlPesquisa = new javax.swing.JPanel();
        panDataGrid1 = new eSchool.GUI.Controls.panDataGrid();
        pnlDetalhe = new javax.swing.JPanel();
        panDetalhe1 = new eSchool.GUI.Controls.panDetalhe();
        panUserPassword1 = new eSchool.GUI.Controls.panUserPassword();
        btnGravar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        javax.swing.GroupLayout pnlPesquisaLayout = new javax.swing.GroupLayout(pnlPesquisa);
        pnlPesquisa.setLayout(pnlPesquisaLayout);
        pnlPesquisaLayout.setHorizontalGroup(
            pnlPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPesquisaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panDataGrid1, javax.swing.GroupLayout.DEFAULT_SIZE, 636, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlPesquisaLayout.setVerticalGroup(
            pnlPesquisaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlPesquisaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panDataGrid1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabConsulta.addTab("Pesquisa", pnlPesquisa);

        btnGravar.setText("Gravar"); // NOI18N
        btnGravar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGravarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar"); // NOI18N
        btnCancelar.setPreferredSize(new java.awt.Dimension(65, 23));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlDetalheLayout = new javax.swing.GroupLayout(pnlDetalhe);
        pnlDetalhe.setLayout(pnlDetalheLayout);
        pnlDetalheLayout.setHorizontalGroup(
            pnlDetalheLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDetalheLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDetalheLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDetalheLayout.createSequentialGroup()
                        .addComponent(panDetalhe1, javax.swing.GroupLayout.DEFAULT_SIZE, 636, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(pnlDetalheLayout.createSequentialGroup()
                        .addComponent(panUserPassword1, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)
                        .addGap(229, 229, 229))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDetalheLayout.createSequentialGroup()
                        .addComponent(btnGravar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        pnlDetalheLayout.setVerticalGroup(
            pnlDetalheLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDetalheLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panDetalhe1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDetalheLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDetalheLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panUserPassword1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(45, 45, 45))
                    .addGroup(pnlDetalheLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                        .addGroup(pnlDetalheLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnGravar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );

        tabConsulta.addTab("Detalhe", pnlDetalhe);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 661, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(tabConsulta, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnGravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGravarActionPerformed
        Boolean eValido = true;
        String mensagemErro = TextHelper.EmptyString;
        
        TableModel resultadoTableModel = panDetalhe1.getDataSource();
        
        if(idObrigatorio && resultadoTableModel.getValueAt(0, 0).toString().equals(TextHelper.EmptyString)){
            eValido = false;
            mensagemErro += "O identificador é obrigatório. \n";     
        }
        
        if(nomeObrigatorio && resultadoTableModel.getValueAt(0, 8).toString().equals(TextHelper.EmptyString)){
            eValido = false;
            mensagemErro = "O nome é obrigatório. \n";
        }
                
        if(eValido){
            uiConector.gravaDetalhe(resultadoTableModel);
            this.setSelectedTab(0);
        }
        else{
            JOptionPane.showMessageDialog(this, mensagemErro);
        }
    }//GEN-LAST:event_btnGravarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGravar;
    private eSchool.GUI.Controls.panDataGrid panDataGrid1;
    private eSchool.GUI.Controls.panDetalhe panDetalhe1;
    private eSchool.GUI.Controls.panUserPassword panUserPassword1;
    private javax.swing.JPanel pnlDetalhe;
    private javax.swing.JPanel pnlPesquisa;
    private javax.swing.JTabbedPane tabConsulta;
    // End of variables declaration//GEN-END:variables
    
    
    @Override
    public void GridDoubleClickEvent(Object sender, Object obj) {
        try {
            DataGrid dg = (DataGrid) sender;
                       
            int row = ((Integer) obj).intValue();
            
            String num = TextHelper.EmptyString;
            if(dg.getValueAt(row, 0) != null)
                num = dg.getValueAt(row, 0).toString();

            CriteriosPesquisa cr = new CriteriosPesquisa();
            cr.add("num",num);
            
            TableModel tableModel = uiConector.pesquisaDetalhe(cr);
            
            this.panDetalhe1.setDataSource(tableModel);
            this.panUserPassword1.setDataSource(tableModel);
            this.setSelectedTab(1);
        
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível identificar o objecto escolhido. \n" + ex.getMessage()); 
            Logger.getLogger(panTabFicha.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void SearchButtonClickEvent(Object Sender, Object obj) {
        try {
            
            CriteriosPesquisa cr = (CriteriosPesquisa)obj;
            
            this.panDataGrid1.setDataSource(uiConector.pesquisaLista(cr));
            this.panDataGrid1.DataBind();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível obter o objecto escolhido. \n" + ex.getMessage()); 
            Logger.getLogger(panDataGrid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void setSelectedTab(int index){
            this.tabConsulta.setSelectedIndex(index);
    }
    
    public void setObjectoTrabalho(IUIConector uiConector){
        try {
            this.uiConector = uiConector;
            this.panDetalhe1.setDataSource(this.uiConector.getDetalhe());
            this.panUserPassword1.setDataSource(this.uiConector.getDetalhe());
            
            this.panDataGrid1.setDataSource(this.uiConector.getLista());
            this.panDataGrid1.DataBind();
    
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível obter o objecto. \n" + ex.getMessage()); 
            Logger.getLogger(panTabFicha.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
      public void setObjectoTrabalho(UIConectorFuncionario uiConector){
        try {
            this.uiConector = uiConector;
            this.panDetalhe1.setDataSource(this.uiConector.getDetalhe());
            this.panUserPassword1.setDataSource(this.uiConector.getDetalhe());
            DataGridColumnIdentifiers dcs = new DataGridColumnIdentifiers();
           
            dcs.add("Número", "idFuncionario", 50);
            panDetalhe1.permiteEditarID(false);
            panUserPassword1.setVisible(false);
                        
            dcs.add("Nome", "nome", 250);
            this.panDataGrid1.setDataGridColumnIdentifiers(dcs);
            this.panDataGrid1.setDataSource(this.uiConector.getLista());
            this.panDataGrid1.DataBind();
            
            nomeObrigatorio = true;
    
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível obter o objecto. \n" + ex.getMessage()); 
            Logger.getLogger(panTabFicha.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    public void setObjectoTrabalho(UIConectorDocente uiConector){
        try {
            this.uiConector = uiConector;
            this.panDetalhe1.setDataSource(this.uiConector.getDetalhe());
            this.panUserPassword1.setDataSource(this.uiConector.getDetalhe());
            DataGridColumnIdentifiers dcs = new DataGridColumnIdentifiers();
            
            dcs.add("Número", "idDocente", 50);
            panDetalhe1.permiteEditarID(false);
            panUserPassword1.setVisible(false);
            
            dcs.add("Nome", "nome", 250);
            this.panDataGrid1.setDataGridColumnIdentifiers(dcs);
            this.panDataGrid1.setDataSource(this.uiConector.getLista());
            this.panDataGrid1.DataBind();
            
            nomeObrigatorio = true;
    
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível obter o objecto. \n" + ex.getMessage()); 
            Logger.getLogger(panTabFicha.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
     public void setObjectoTrabalho(UIConectorAluno uiConector){
        try {
            this.uiConector = uiConector;
            this.panDetalhe1.setDataSource(this.uiConector.getDetalhe());
            this.panUserPassword1.setDataSource(this.uiConector.getDetalhe());
            DataGridColumnIdentifiers dcs = new DataGridColumnIdentifiers();
          
            dcs.add("Número", "numAluno",50);
            panDetalhe1.permiteEditarID(true);
            panUserPassword1.setVisible(false);
            
            dcs.add("Nome", "nome", 250);
            this.panDataGrid1.setDataGridColumnIdentifiers(dcs);
            this.panDataGrid1.setDataSource(this.uiConector.getLista());
            this.panDataGrid1.DataBind();
            
            nomeObrigatorio = true;
            idObrigatorio = true;
    
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível obter o objecto. \n" + ex.getMessage()); 
            Logger.getLogger(panTabFicha.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
